<?php

namespace Frieza\Modules\Course\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Biz\model\course\Course as mCourse;
use Biz\model\course\CourseOrder as mCourseOrder;
use Biz\pockets\constInterface\PayConfig;
use Biz\service\course\AdmCourseOrder;
use Doraemon\service\User;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    public function index(Request $request, Content $content)
    {
        $pageSize = $request->input("per_page", 20);
        $page = $request->input("page", 1);
        if (!$request->input('addtime')) {
            $request->merge([
                'addtime' => [
                    'start' => date('Y-m-d', strtotime("-7days")),
                    'end'   => date('Y-m-d', strtotime('+1days')),
                ]
            ]);
        }
        $condition = $this->_index_condition($request->all());
        $where = $condition;
        $where['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $where['ORDER'] = [
            'id' => 'DESC'
        ];
        $result = AdmCourseOrder::getOrderList($where);
        $gridModel = new pGridDataModel($result['list'], $result['total'], [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $condition) {
                $grid->disableCreateButton();
                $grid->paginate($gridModel->getPerPage());
                $grid->filter(function (Grid\Filter $filter) use ($condition) {
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function (Grid\Filter $filter) use ($condition) {
                        $filter->equal('order_no', '订单号');
                        $filter->equal('order_status', '订单状态')->select(mCourseOrder::STATUS_DESC);
                        $filter->between('addtime', '下单时间')->datetime();
                    });
                    $filter->column(1 / 2, function ($filter) {
                        $filter->dropDown('course', [
                            'course_id' => '课程id',
                            'title'     => '课程标题',
                        ]);
                        $filter->dropDown('user', [
                            'uid'  => '用户uid',
                            'nick' => '用户昵称',
                        ]);
                    });
                    if (!empty($condition)) {
                        $filter->expand();//默认展开
                    }
                });
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->column('order_no', '订单号');
                $grid->column('course_id', '课程id');
                $grid->column('course_title', '课程标题');
                $grid->column('buy_price', '课程售价');
                $grid->column('uid', '购买人')->display(function () {
                    return '<p>' . $this->uid . '</p>' . '<p>' . $this->nick . '</p>';
                });
                $grid->column('status_desc', '订单状态');
                $grid->column('pay_amount', '支付金额');
                $grid->column('pay_way', '支付渠道')->display(function () {
                    return PayConfig::_PAY_WAYS_DESC[$this->pay_way];
                });
                $grid->column('addtime', '下单时间');
                $grid->disableActions();
            }
        );
        return $content->header('订单列表')->breadcrumb(
            ['text' => '订单列表'])
            ->row($gridList->render());
    }

    public function daily(Request $request, Content $content)
    {
        $pageSize = $request->input("per_page", 20);
        $page = $request->input("page", 1);
        $where = $this->_index_condition($request->all());
        $where['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $result = AdmCourseOrder::getCourseDaily($where);
        $gridModel = new pGridDataModel($result);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->disableCreateButton();
                $grid->paginate($gridModel->getPerPage());
                $grid->disableFilter();
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->column('course_title', '课程标题');
                $grid->column('course_id', '课程id');
                $grid->column('price', '售价');
                $grid->column('yesterday_count', '昨日售出数量');
                $grid->column('yesterday_income', '昨日收入');
                $grid->column('total_count', '总售出数量');
                $grid->column('total_income', '总收入');
                $grid->column('online_time', '上线时间');
                $grid->disableActions();
                $grid->disablePagination();
            }
        );
        return $content->header('销售日报')->breadcrumb(
            ['text' => '销售日报'])
            ->row($gridList->render());
    }

    public function _index_condition($request)
    {
        $response = [];
        if (isset($request['order_no']) && strlen($request['order_no']) > 0) {
            $response['order_no'] = $request['order_no'];
            return $response;
        }
        if (isset($request['order_status']) && ($request['order_status'] > 0)) {
            $response['order_status'] = $request['order_status'];
        }
        if (isset($request['course_id']) && ($request['course_id'] > 0)) {
            $response['course_id'] = $request['course_id'];
        }
        if (isset($request['title']) && strlen($request['title']) > 0) {
            $courseIds = mCourse::selectByWhere(['title[~]' => $request['title']], 'course_id');
            if ($courseIds) {
                $response['course_id'] = $courseIds;
            }
        }
        if (isset($request['uid']) && $request['uid'] > 0) {
            $response['uid'] = $request['uid'];
        }
        if (isset($request['nick']) && strlen($request['nick']) > 0) {
            $uid = User::getUidByNick($request['nick']);
            $response['uid'] = $uid;
        }
        if (isset($request['addtime'])) {
            if (isset($request['addtime']['start']) && strlen($request['addtime']['start']) > 0) {
                $response['addtime[>=]'] = $request['addtime']['start'];
            }
            if (isset($request['addtime']['end']) && strlen($request['addtime']['end']) > 0) {
                $response['addtime[<=]'] = $request['addtime']['end'];
            }
        }
        return $response;
    }

}